//
//  SinaTabBarController.swift
//  SinaWeibo(Swift)
//
//  Created by 王忠迪 on 9/22/16.
//  Copyright © 2016 王忠迪. All rights reserved.
//

import UIKit

class SinaTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
//        在这里设置颜色下面就可以不用设置图片和文字的颜色
//        self.tabBar.tintColor = UIColor.orange
        
        //这种方式无法赋值,需要使用KVC
        //self.tabBar = SinaTabBar()
        
        let sinaTabBar = SinaTabBar()
        
        sinaTabBar.composeClosure = {
            
            print("按钮被点击")
            print("😁😁😁😁😁😁😁😁😁😁😁😁😁")
        }
        
        self.setValue(sinaTabBar, forKey: "tabBar")
        
        addChildViewControllers()
    }
    
    
    func addChildViewControllers() {
        
        addChildViewController(vc: SinaHomeTableViewController(), title: "首页", image: "tabbar_home")
        
        addChildViewController(vc: SinaMessageTableViewController(), title: "消息", image: "tabbar_message_center")
        
        addChildViewController(vc: SinaDiscoverTableViewController(), title: "发现", image: "tabbar_discover")
        
        addChildViewController(vc: SinaProfileTableViewController(), title: "我", image: "tabbar_profile")
        
    }
    
    func addChildViewController(vc: UIViewController, title:String ,image: String) {
        //设置navigationItem的名称
        vc.navigationItem.title = title
        //设置tabBarItem的名称
        vc.tabBarItem.title = title
        //设置选中是文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        //设置文字上移,美化UI界面
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
        //设置文字大小,默认是9号字
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 10)], for: .normal)
        //设置tabBarItem图片
        vc.tabBarItem.image = UIImage(named: image)
        //设置选中图片不渲染,保持原色
        vc.tabBarItem.selectedImage = UIImage(named: image + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置徽标的颜色
        vc.tabBarItem.badgeColor = UIColor.blue
        //设置徽标的个数
//        vc.tabBarItem.badgeValue = "3"
        //图片位移
//        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(-5, 0, 5, 0)
        let nav = SinaBaseNavigationController(rootViewController: vc)
        self.addChildViewController(nav)
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
